import { request } from '@umijs/max';
import { setAuthHeader } from '../utils/authInterceptor';

/**
 * 获取所有物资列表
 * @returns 所有物资列表数据
 */
export async function getMaterialList() {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request('/api/material/list', {
    method: 'GET',
    headers,
  });
}

/**
 * 获取物资列表（分页）
 * @param params 查询参数
 * @returns 物资列表数据
 */
export async function getMaterialListPaginated(params: MATERIAL.MaterialListParams) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request<MATERIAL.MaterialListResponse>('/api/material/pagedList', {
    method: 'GET',
    headers,
    params,
  });
}

/**
 * 添加物资
 * @param params 物资信息
 * @returns 添加结果
 */
export async function addMaterial(params: Omit<MATERIAL.MaterialModel, 'id' | 'createTime' | 'updateTime' | 'lastInventoryTime'>) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request('/api/material/add', {
    method: 'POST',
    headers,
    data: params,
  });
}

/**
 * 更新物资信息
 * @param params 物资信息
 * @returns 更新结果
 */
export async function updateMaterial(params: Partial<MATERIAL.MaterialModel> & { id: number }) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request('/api/material/update', {
    method: 'PUT',
    headers,
    data: params,
  });
}

/**
 * 删除物资
 * @param id 物资ID
 * @returns 删除结果
 */
export async function removeMaterial(id: number) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request(`/api/material/delete/${id}`, {
    method: 'DELETE',
    headers,
  });
}

/**
 * 获取物资详情
 * @param id 物资ID
 * @returns 物资详情
 */
export async function getMaterialDetail(id: number) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request(`/api/material/get/${id}`, {
    method: 'GET',
    headers,
  });
}